<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>药品识别 - 药品识别系统</title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .navbar {
            background: rgba(255, 255, 255, 0.95) !important;
            backdrop-filter: blur(10px);
            box-shadow: 0 2px 20px rgba(0,0,0,0.1);
        }
        
        .main-container {
            margin-top: 80px;
            padding: 20px;
        }
        
        .card {
            border: none;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            backdrop-filter: blur(10px);
            background: rgba(255, 255, 255, 0.9);
        }
        
        .btn-primary {
            background: linear-gradient(45deg, #667eea, #764ba2);
            border: none;
            border-radius: 25px;
            padding: 10px 30px;
            transition: all 0.3s ease;
        }
        
        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        
        .upload-area {
            border: 2px dashed #667eea;
            border-radius: 15px;
            padding: 40px;
            text-align: center;
            transition: all 0.3s ease;
            cursor: pointer;
            background: rgba(102, 126, 234, 0.05);
            min-height: 200px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }
        
        .upload-area:hover {
            border-color: #764ba2;
            background: rgba(118, 75, 162, 0.1);
        }
        
        .upload-area.dragover {
            border-color: #28a745;
            background: rgba(40, 167, 69, 0.1);
        }
        
        .result-card {
            margin-top: 20px;
            animation: slideInUp 0.5s ease;
        }
        
        @keyframes slideInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .loading {
            display: none;
        }
        
        .spinner-border {
            color: #667eea;
        }
        
        .preview-image {
            max-width: 100%;
            max-height: 300px;
            border-radius: 10px;
            margin-top: 15px;
        }
        
        @media (max-width: 768px) {
            .main-container {
                margin-top: 70px;
                padding: 10px;
            }
            
            .upload-area {
                padding: 20px;
                min-height: 150px;
            }
        }
    </style>
</head>
<body>
    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-light fixed-top">
        <div class="container">
            <a class="navbar-brand fw-bold" href="/">
                <i class="fas fa-pills text-primary me-2"></i>药品识别系统
            </a>
            
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/"><i class="fas fa-home me-1"></i>首页</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/recognition"><i class="fas fa-camera me-1"></i>药品识别</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/history"><i class="fas fa-history me-1"></i>识别历史</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/drugs"><i class="fas fa-database me-1"></i>药品管理</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- Main Content -->
    <div class="container main-container">
        <div class="row justify-content-center">
            <div class="col-lg-8">
                <div class="card">
                    <div class="card-header text-center bg-transparent">
                        <h4 class="mb-0">
                            <i class="fas fa-camera text-primary me-2"></i>药品图片识别
                        </h4>
                        <small class="text-muted">上传药品图片，AI将自动识别药品种类和数量</small>
                    </div>
                    <div class="card-body">
                        <form id="recognitionForm" enctype="multipart/form-data">
                            <div class="upload-area" id="uploadArea">
                                <i class="fas fa-cloud-upload-alt fa-3x text-muted mb-3"></i>
                                <h5 class="text-muted">点击或拖拽上传图片</h5>
                                <p class="text-muted mb-3">支持 JPG、PNG、GIF 格式，最大 10MB</p>
                                <input type="file" id="imageInput" name="image" accept="image/*" style="display: none;">
                                <button type="button" class="btn btn-outline-primary" onclick="document.getElementById('imageInput').click()">
                                    <i class="fas fa-folder-open me-2"></i>选择文件
                                </button>
                            </div>
                            
                            <div id="imagePreview" style="display: none;">
                                <img id="previewImg" class="preview-image" alt="预览图片">
                                <div class="text-center mt-3">
                                    <button type="submit" class="btn btn-primary btn-lg">
                                        <i class="fas fa-search me-2"></i>开始识别
                                    </button>
                                    <button type="button" class="btn btn-outline-secondary ms-2" onclick="resetForm()">
                                        <i class="fas fa-redo me-2"></i>重新选择
                                    </button>
                                </div>
                            </div>
                        </form>
                        
                        <!-- Loading -->
                        <div class="loading text-center py-4" id="loadingDiv">
                            <div class="spinner-border" role="status">
                                <span class="visually-hidden">识别中...</span>
                            </div>
                            <p class="mt-2 text-muted">AI正在分析图片，请稍候...</p>
                        </div>
                        
                        <!-- Results -->
                        <div id="resultDiv" style="display: none;">
                            <div class="result-card">
                                <div class="card border-success">
                                    <div class="card-header bg-success text-white">
                                        <h5 class="mb-0">
                                            <i class="fas fa-check-circle me-2"></i>识别结果
                                        </h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="row">
                                            <div class="col-md-6">
                                                <h6 class="text-primary">识别到的药品：</h6>
                                                <p id="recognizedDrugs" class="mb-2"></p>
                                            </div>
                                            <div class="col-md-6">
                                                <h6 class="text-primary">药品数量：</h6>
                                                <p id="drugCount" class="mb-2"></p>
                                            </div>
                                        </div>
                                        <div class="row mt-3">
                                            <div class="col-12">
                                                <h6 class="text-primary">识别时间：</h6>
                                                <p id="recognitionTime" class="mb-0"></p>
                                            </div>
                                        </div>
                                        <div class="text-center mt-3">
                                            <button type="button" class="btn btn-primary" onclick="resetForm()">
                                                <i class="fas fa-plus me-2"></i>继续识别
                                            </button>
                                            <a href="/history" class="btn btn-outline-info ms-2">
                                                <i class="fas fa-history me-2"></i>查看历史
                                            </a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <script>
        $(document).ready(function() {
            setupDragAndDrop();
            setupFileInput();
            setupForm();
        });
        
        function setupDragAndDrop() {
            const uploadArea = $('#uploadArea');
            
            uploadArea.on('dragover', function(e) {
                e.preventDefault();
                $(this).addClass('dragover');
            });
            
            uploadArea.on('dragleave', function(e) {
                e.preventDefault();
                $(this).removeClass('dragover');
            });
            
            uploadArea.on('drop', function(e) {
                e.preventDefault();
                $(this).removeClass('dragover');
                
                const files = e.originalEvent.dataTransfer.files;
                if (files.length > 0) {
                    handleFileSelect(files[0]);
                }
            });
            
            uploadArea.on('click', function() {
                $('#imageInput').click();
            });
        }
        
        function setupFileInput() {
            $('#imageInput').on('change', function(e) {
                if (e.target.files.length > 0) {
                    handleFileSelect(e.target.files[0]);
                }
            });
        }
        
        function handleFileSelect(file) {
            if (!file.type.startsWith('image/')) {
                showAlert('请选择图片文件！', 'warning');
                return;
            }
            
            if (file.size > 10 * 1024 * 1024) {
                showAlert('文件大小不能超过10MB！', 'warning');
                return;
            }
            
            const reader = new FileReader();
            reader.onload = function(e) {
                $('#previewImg').attr('src', e.target.result);
                $('#uploadArea').hide();
                $('#imagePreview').show();
            };
            reader.readAsDataURL(file);
        }
        
        function setupForm() {
            $('#recognitionForm').on('submit', function(e) {
                e.preventDefault();
                
                const formData = new FormData();
                const fileInput = $('#imageInput')[0];
                
                if (!fileInput.files.length) {
                    showAlert('请先选择图片！', 'warning');
                    return;
                }
                
                formData.append('image', fileInput.files[0]);
                
                // 显示加载状态
                $('#imagePreview').hide();
                $('#loadingDiv').show();
                $('#resultDiv').hide();
                
                // 发送请求
                $.ajax({
                    url: '/api/recognize',
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function(response) {
                        displayResult(response);
                    },
                    error: function(xhr, status, error) {
                        console.error('识别失败:', error);
                        showAlert('识别失败，请重试！', 'danger');
                        $('#loadingDiv').hide();
                        $('#imagePreview').show();
                    }
                });
            });
        }
        
        function displayResult(result) {
            $('#recognizedDrugs').text(result.recognizedDrugs || '未识别到药品');
            $('#drugCount').text(result.drugCount + ' 个');
            $('#recognitionTime').text(formatDateTime(result.recognitionTime));
            
            $('#loadingDiv').hide();
            $('#resultDiv').show();
        }
        
        function resetForm() {
            $('#recognitionForm')[0].reset();
            $('#uploadArea').show();
            $('#imagePreview').hide();
            $('#loadingDiv').hide();
            $('#resultDiv').hide();
        }
        
        function showAlert(message, type = 'info') {
            const alertHtml = `
                <div class="alert alert-${type} alert-dismissible fade show" role="alert">
                    ${message}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
            `;
            $('.main-container').prepend(alertHtml);
            
            setTimeout(() => {
                $('.alert').alert('close');
            }, 3000);
        }
        
        function formatDateTime(dateTimeString) {
            const date = new Date(dateTimeString);
            return date.toLocaleString('zh-CN');
        }
    </script>
</body>
</html>